﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZhouYu.NewWorld.BLL.IService;
using ZhouYu.NewWorld.DAL;
using ZhouYu.NewWorld.Model;
using ZhouYu.NewWorld.ViewModel;

namespace ZhouYu.NewWorld.BLL.Service
{
    public class GoodsService : BaseRepository<Goods>, IGoodsService
    {
        /// <summary>
        /// 多表连查
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public ResponseResult GetGoodsInfo(int page,int limit)
        {
            ResponseResult result = new ResponseResult();
            //创建实例            
            using (var db = new SyDbContext())
            {
                //两表连接查询
                var good = from a in db.Goods
                           join b in db.GoodsDetails
                           on a.GoodsId equals b.GoodsId
                           select new GoodsCustom() //自定义实体类 包括需要展示的字段
                           {
                               //赋值
                               GoodsId = a.GoodsId,
                               GoodsDetails_Id = b.GoodsDetails_Id,
                               GoodsNo = a.GoodsNo,
                               GoodsName = a.GoodsName,
                               Describe = a.Describe,
                               Price = a.Price,
                               Discount = a.Discount,
                               Weight = a.Weight,
                               SupplierId = a.SupplierId,
                               BrandId = a.BrandId,
                               ProductNo = b.ProductNo,
                               ProductCode = b.ProductCode,
                               GoodsLevel = b.GoodsLevel,
                               ProductDate = b.ProductDate,
                               ValidityTime = b.ValidityTime 
                           };
                //responseResult对象赋值
                result.data = good.ToList().Skip((page - 1) * limit).Take(limit);
                result.count = good.ToList().Count;
                result.msg = string.Empty;
                result.code = 0;
            }
            return result;
        }

        /// <summary>
        /// 多表添加
        /// </summary>
        /// <param name="goodsCustom"></param>
        /// <returns></returns>
        public ResponseResult AddGoods(GoodsCustom goodsCustom)
        {
            ResponseResult result = new ResponseResult();
            //创建实例
            using (var db = new SyDbContext())
            {
                //开启事务
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        //商品
                        Goods goods = new Goods();
                        goods.GoodsNo = goodsCustom.GoodsNo;
                        goods.GoodsName = goodsCustom.GoodsName;
                        goods.Describe = goodsCustom.Describe;
                        goods.Price = goodsCustom.Price;
                        goods.Discount = goodsCustom.Discount;
                        goods.Weight = goodsCustom.Weight;
                        goods.SupplierId = goodsCustom.SupplierId;
                        goods.BrandId = goodsCustom.BrandId;
                        goods.Goods_CreateTime = DateTime.Now;
                        goods.Goods_UpdateTime = DateTime.Now;
                        //添加商品表-- db.表名.Add传入对象
                        db.Goods.Add(goods);
                        //保存
                        db.SaveChanges();

                        //商品详细
                        GoodsDetails goodsDetails = new GoodsDetails();
                        goodsDetails.GoodsId = goods.GoodsId;
                        goodsDetails.ProductNo = goodsCustom.ProductNo;
                        goodsDetails.ProductCode = goodsCustom.ProductCode;
                        goodsDetails.GoodsLevel = goodsCustom.GoodsLevel;
                        goodsDetails.ProductDate = goodsCustom.ProductDate;
                        goodsDetails.ValidityTime = goodsCustom.ValidityTime;
                        goodsDetails.GoodsDet_CreateTime = DateTime.Now;
                        goodsDetails.GoodsDet_UpdateTime = DateTime.Now;
                        //添加商品详细表
                        db.GoodsDetails.Add(goodsDetails);
                        //保存
                        db.SaveChanges();
                        //提交
                        tran.Commit();
                        result.Success = true;
                    }
                    catch (Exception ex)
                    {
                        //回滚
                        tran.Rollback();
                        result.Success = false;
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// 多表编辑
        /// </summary>
        /// <param name="goodsCustom"></param>
        /// <returns></returns>
        public ResponseResult UpdateGoods(GoodsCustom goodsCustom)
        {
            ResponseResult result = new ResponseResult();
            //创建实例
            using (var db = new SyDbContext())
            {
                //开启事务
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        //酒店
                        Goods goods = db.Goods.Find(goodsCustom.GoodsId);
                        if (goods != null)
                        {
                            goods.GoodsNo = goodsCustom.GoodsNo;
                            goods.GoodsName = goodsCustom.GoodsName;
                            goods.Describe = goodsCustom.Describe;
                            goods.Price = goodsCustom.Price;
                            goods.Discount = goodsCustom.Discount;
                            goods.Weight = goodsCustom.Weight;
                            goods.SupplierId = goodsCustom.SupplierId;
                            goods.BrandId = goodsCustom.BrandId;
                            goods.Goods_CreateTime = DateTime.Now;
                            goods.Goods_UpdateTime = DateTime.Now;
                            //修改商品表
                            db.Entry(goods).State = EntityState.Modified;
                        }

                        //酒店详细
                        GoodsDetails goodsDetails = db.GoodsDetails.Find(goodsCustom.GoodsDetails_Id);
                        if (goodsDetails != null)
                        {
                            goodsDetails.GoodsId = goods.GoodsId;
                            goodsDetails.ProductNo = goodsCustom.ProductNo;
                            goodsDetails.ProductCode = goodsCustom.ProductCode;
                            goodsDetails.GoodsLevel = goodsCustom.GoodsLevel;
                            goodsDetails.ProductDate = goodsCustom.ProductDate;
                            goodsDetails.ValidityTime = goodsCustom.ValidityTime;
                            goodsDetails.GoodsDet_CreateTime = DateTime.Now;
                            goodsDetails.GoodsDet_UpdateTime = DateTime.Now;
                            //修改商品详情表
                            db.Entry(goodsDetails).State = EntityState.Modified;
                        }
                        //保存
                        db.SaveChanges();

                        //提交
                        tran.Commit();
                        result.Success = true;
                    }
                    catch (Exception ex)
                    {
                        //回滚
                        tran.Rollback();
                        result.Success = false;
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// 多表删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ResponseResult DeleteGoods(int id)
        {
            ResponseResult result = new ResponseResult();
            Goods goods = new Goods();
            goods.GoodsId = id;
            using (var db = new SyDbContext())
            {
                //开启事务
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        List<GoodsDetails> goodsDetails = db.GoodsDetails.Where(a => a.GoodsId == id).ToList();
                        //删除商品表
                        db.Entry(goods).State = EntityState.Deleted;

                        //循环删除当前商品下的信息记录
                        for (int i = 0; i < goodsDetails.Count; i++)
                        {
                            //根据主键id获取单个实体
                            var entity = db.GoodsDetails.Find(goodsDetails[i].GoodsDetails_Id);
                            //如果实体不为空则进行删除
                            if (entity != null)
                            {
                                //删除
                                db.GoodsDetails.Remove(entity);
                            }
                        }
                        //保存
                        db.SaveChanges();
                        //提交
                        tran.Commit();
                        result.Success = true;
                    }
                    catch (Exception ex)
                    {
                        //回滚
                        tran.Rollback();
                        result.Success = false;
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// 获取品牌对象
        /// </summary>
        /// <returns></returns>
        public ResponseResult GetBrand() 
        {
            ResponseResult result = new ResponseResult();
            using (var db = new SyDbContext())
            {
                //查询品牌表
                List<Brand> brands = db.Brand.Where(a => true).ToList();
                result.data = brands;
            }
            return result;
        }

        /// <summary>
        /// 获取供应商对象
        /// </summary>
        /// <returns></returns>
        public ResponseResult GetSupplier()
        {
            ResponseResult result = new ResponseResult();
            using (var db = new SyDbContext())
            {
                //查询供应商表
                List<Supplier> suppliers = db.Supplier.Where(a => true).ToList();
                result.data = suppliers;
            }
            return result;
        }
    }
}
